package io.gitee.hefren.hhregistry.http;

import com.google.gson.Gson;

import java.util.Objects;

public interface HttpInvoker {

    HttpInvoker DEFAULT = new OkHttpInvoker(5000);
    String get(String url);
    String post(String url, String requestString);

    static <T> T getHttp(String url, Class<T> clazz) {
        try {
            String result = DEFAULT.get(url);
            if (Objects.isNull(result)) {
                return null;
            }
            return new Gson().fromJson(result, clazz);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    static <T> T postHttp(String url, String requestString, Class<T> clazz) {
        try {
            String result = DEFAULT.post(url, requestString);
            if (Objects.isNull(result)) {
                return null;
            }
            return new Gson().fromJson(result, clazz);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
